package com.lianxi.controller;

import com.lianxi.util.FtpUtil;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@RestController
@RequestMapping("/ftp")
public class FtpController {

    @PostMapping("/upload")
    @ResponseBody
    public Object upload(@RequestParam("file") MultipartFile[] file) throws IOException {
        Map<String,Object> result = new HashMap<>();
        result.put("errno",0);
        List<Map<String,Object>> data = new ArrayList<>();
        for(MultipartFile multipartFile : file){
            String fileName = multipartFile.getOriginalFilename();
            String destPath = FtpUtil.getUploadPath(multipartFile);
            FtpUtil.upload(destPath,fileName,multipartFile.getInputStream());
            Map<String,Object> tmp = new HashMap<>();
            tmp.put("destPath",destPath);
            tmp.put("fileName",fileName);
            data.add(tmp);
        }
        result.put("data",data);
        return result;
    }

    @RequestMapping("/download")
    public void download(@RequestParam String destPath,@RequestParam String fileName, HttpServletResponse response) throws IOException {
        response.setHeader("content-disposition", "attachment;filename="
                + URLEncoder.encode(fileName, "UTF-8"));
        FtpUtil.download(destPath,fileName,response.getOutputStream());
    }

    @Test
    public void test(){
        String s="1,2,3";
        boolean contains1 = Arrays.asList(s.split(",")).contains("1");
        System.out.println(contains1);
    }


}
